package com.ruoyi.socket.server;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.gams.domain.GamsDevice;
import com.ruoyi.gams.domain.GamsUser;
import com.ruoyi.gams.domain.GamsUserQueue;
import com.ruoyi.gams.mapper.GamsDeviceMapper;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * socketio client 操作组件
 *
 * @author haishui211
 */
@Component
@Slf4j
public class SocketClientStore
{

    private Map<String, ChannelHandlerContext> clients = new HashMap<>();

    @Autowired
    private GamsDeviceMapper deviceMapper;
    @Value("${ruoyi.print.title}")
    private String title;
    @Value("${ruoyi.print.tips}")
    private String tips;
    @Value("${ruoyi.print.qrcodeurl}")
    private String qrcodeurl;

    /**
     * 保存socketio client 客户端
     *
     * @param client
     */
    public void storeClientId(ChannelHandlerContext client)
    {
        clients.put(client.channel().id().toString(), client);
    }


    /**
     * 移除socketio client 客户端
     */
    public void delClientId(String id)
    {
        clients.remove(id);
    }

    public int countClient()
    {
        return clients.keySet().size();
    }


    public void send(String deviceId, GamsUserQueue userQueue, GamsUser user)
    {
        JSONObject res = new JSONObject();
        res.put("d", "S000001");
        res.put("c", "3");//3.打印
        res.put("b", userQueue.getBussinessName());
        res.put("count", userQueue.getQueueUserCount());
        res.put("number", userQueue.getNumber());
        String certnumber = user.getCertnumber();
        if (certnumber.length() > 10)
        {
            certnumber = certnumber.substring(0, 4) + "********" + certnumber.substring(certnumber.length() - 4);
        }
        res.put("cardid", certnumber);
        res.put("tips", tips);
        res.put("projectName", title);
        res.put("qrcodeUrl", qrcodeurl + user.getId());
        String name = user.getName();
        String userName="";
        if (name.length() > 1)
        {
            userName = name.substring(0, 1) + "*";
            if (user.getName().length() > 2)
            {
                userName += name.substring(name.length() - 1);
            }
        }
        res.put("userName", userName);
        send(deviceId, res.toJSONString());
    }


    public void send(String deviceId, String msg)
    {
        GamsDevice firstByDeviceId = deviceMapper.findFirstByDeviceId(deviceId);
        if (firstByDeviceId == null)
        {
            log.info("找不到设备");
            return;
        }
        String clientKey = firstByDeviceId.getClientKey();
        ChannelHandlerContext ctx = clients.get(clientKey);
        if (ctx == null)
        {
            return;
        }
        send(ctx, msg);
    }

    public void send(ChannelHandlerContext ctx, String msg)
    {
        ctx.channel().writeAndFlush(new TextWebSocketFrame(msg));//向请求客户端输出消息
    }

    public void sendHeart(ChannelHandlerContext ctx)
    {
        JSONObject res = new JSONObject();
        res.put("d", "S000001");
        res.put("c", "2");
        send(ctx, JSONObject.toJSONString(res));
    }
}
